package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {

    @RequestMapping("showUser")
    @ResponseBody
    public Result showUser(User user) {
        //通过调用LoginRealm进行认证
        //1.获取subject对象
        Subject subject = SecurityUtils.getSubject();
        //2.将我们的传来的用户名,密码包,装成传来的shiro的token对象;
        UsernamePasswordToken token = new UsernamePasswordToken(user.getPname(), user.getPwd());
        //3.调用subject的login方法执行登录逻辑
        try {
            subject.login(token);
        } catch (UnknownAccountException e) {
            return Result.response(ResultEnum.ERROEUSER);
        } catch (IncorrectCredentialsException e) {
            return Result.response(ResultEnum.ERRORPWD);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
}
